import Vue from "vue";
import Vuex from 'vuex'
const countOptions ={
    namespaced:true,
    actions:{
        addDelayN(context, val) {
            setTimeout(() => {
                context.commit('ADDN', val)
            }, 500)
        },
        addOddN(context, val) {
            if (context.state.num % 2)
                context.commit('ADDN', val)
        }
    },
    mutations:{
        ADDN(state, val) {
            state.num += val
        },
        MINUSN(state, val) {
            state.num -= val
        },
    },
    state:{
        num: 0,
        Sname: `weiwei`,
        subject: 'aini',
    },
    getters:{
        bigNum(state) {
            return state.num * 10
        }
    }
}
const personOpttions ={
    namespaced:true,
    actions:{
        setPersonWang(context,val){
            if(val.name.indexOf('王')===0){
                context.commit.ADDPERSON('ADDPERSON',val)
            }else console.log('不是老王！');
        }
    },
    mutations:{        
        ADDPERSON(state, val) {
            state.personList.unshift(val)
        }

    },
    state:{
        personList: [{ id: '1', name: '张三' }]
    },
    getters:{
        getFirstName(state){
            return state.personList[0].name
        }
    }
}

Vue.use(Vuex)
export default new Vuex.Store({
    modules:{
        countAbout:countOptions,
        personAbout:personOpttions
    }
   
})